/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.builder;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import org.newdawn.slick.SlickException;

import ca.usherbrooke.application.MyTiledMap;
import ca.usherbrooke.builder.entity.EntityModelObject;
import ca.usherbrooke.builder.entity.EntityModelObjectBuilder;
import ca.usherbrooke.builder.equipment.EquipmentModelObject;
import ca.usherbrooke.builder.equipment.EquipmentModelObjectBuilder;
import ca.usherbrooke.builder.feature.WorldModelBuilder;
import ca.usherbrooke.model.world.IWorldModel;
import ca.usherbrooke.view.properties.ViewSlickProperties;

/**
 * @author Cody Stoutenburg
 * 
 */
public class SimulationWrapper implements Serializable {
	private static final long serialVersionUID = 1L;

	public static String EQUIPMENT_VIEW_FILE_PATH = "./props/equipment.txt";
	public static String ENTITY_VIEW_FILE_PATH = "./props/entity.txt";

	public static String EQUIPMENT_FILE_PATH = "./equipment/";
	public static String ENTITY_FILE_PATH = "./entity/";

	private final ViewSlickProperties propertiesFile;
	private final IWorldModel world;

	public SimulationWrapper(String propertiesFilename) throws SlickException,
			IOException {
		this.propertiesFile = new ViewSlickProperties(propertiesFilename);

		MyTiledMap tmap;
		tmap = new MyTiledMap(
				this.propertiesFile.getPath(ViewSlickProperties.TILED_MAP_PATH));

		world = WorldModelBuilder.generateWorld(tmap);

		// create equipement
		File directoryEquipment = new File(
				propertiesFile.getLocalPath(EQUIPMENT_FILE_PATH));
		String[] files = directoryEquipment.list();
		for (String filename : files) {
			File file = new File(directoryEquipment, filename);
			EquipmentModelObject equipementObject = EquipmentModelObjectBuilder
					.createEntityObject(file.getCanonicalPath());
			world.createEquipment(equipementObject);
		}

		// create entity
		File directoryEntity = new File(
				propertiesFile.getLocalPath(ENTITY_FILE_PATH));
		files = directoryEntity.list();
		for (String filename : files) {
			File file = new File(directoryEntity, filename);
			EntityModelObject entityObject = EntityModelObjectBuilder
					.createEntityObject(file.getCanonicalPath());
			world.createEntity(entityObject);
		}

	}

	public IWorldModel getWorld() {
		return world;
	}

	/**
	 * 
	 * @return - the path of the main property file
	 */
	public String getPropertiesPath() {
		return propertiesFile.getFilePath();
	}

	public Integer getEntityCount() {
		return world.getAllEntities().size();
	}
}
